package com.china08.yxyapi.api.home.activity.healthcompetition;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudent;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudentAnswer;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentAnswerRepository;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentRepository;

/**
 * 已答题状态集合
 * 
 * @author zd.Jin
 *
 *
 */
@RestController
@CrossOrigin
public class StudentQuesStatusLstController extends YxyApiBaseController {
	@Autowired
	private YxyCompetitionStudentRepository yxyCompetitionStudentRepository;
	@Autowired
	private YxyCompetitionStudentAnswerRepository yxyCompetitionStudentAnswerRepository;

	@RequestMapping(value = "/api_yw/home/activity/healthcompetition/studentanswer/status/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
		RespModel respModel = new RespModel();
		YxyCompetitionStudent yxyCompetitionStudent = yxyCompetitionStudentRepository
				.findTop1ByStudentId(reqModel.studentId);
		if (yxyCompetitionStudent != null) {
			String stage = yxyCompetitionStudent.getStage();
			if ("小学".equals(stage)) {
				respModel.wenjuan = "小学生健康教育知识竞赛试题";
			} else if ("初中".equals(stage)) {
				respModel.wenjuan = "初高中学生健康教育知识竞赛试题";
			} else if ("高中".equals(stage)) {
				respModel.wenjuan = "初高中学生健康教育知识竞赛试题";
			}
			// 已答题目集合
			List<YxyCompetitionStudentAnswer> answers = yxyCompetitionStudentAnswerRepository
					.findByStudentIdOrderByQuesOrder(yxyCompetitionStudent.getStudentId());
			respModel.quescount = answers.size();
		}
		return respModel;
	}

	protected static class ReqModel extends YxyApiPageReqModel {

		public String studentId;// 学生id

		public void check() {
			Assert.notNull(StringUtils.defaultIfBlank(studentId, null), "学生id不能为空");
		}

	}

	protected static class RespModel extends YxyApiRespModel {
		// 问卷名称
		public String wenjuan;
		// 已答的题目数
		public Integer quescount;
	}

}
